package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Member;
import com.windfone.model.Order;
import com.windfone.model.ReceiveMsg;
import com.windfone.service.MemberService;
import com.windfone.service.OrderService;

public class OrderWindfoneAdmin extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private OrderService orderService;
	private MemberService memberService;
	private String orderStatus;
	private String beginDate;
	private String endDate;
	private List <ReceiveMsg> qryOrders;
	private String orderSeq;
	private ReceiveMsg receiveMsg;
	private List<Order> orderList ;
	private boolean msg;
	private String companyMemo;
	
	public String getCompanyMemo() {
		return companyMemo;
	}

	public void setCompanyMemo(String companyMemo) {
		this.companyMemo = companyMemo;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public boolean isMsg() {
		return msg;
	}

	public void setMsg(boolean msg) {
		this.msg = msg;
	}

	public String getOrderSeq() {
		return orderSeq;
	}
	
	public void setOrderSeq(String orderSeq) {
		this.orderSeq = orderSeq;
	}
	
	public String updateOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    if ( session.getAttribute( "userName") !=null){
	    	ReceiveMsg bean = orderService.getReceiveMsgByOrderId(orderSeq);
	    	ReceiveMsg tmp = orderService.getReceiveMsgByOrderId(orderSeq);
	    	bean.setCompanyMemo(companyMemo);
	    	bean.setStatus(Integer.parseInt(orderStatus));
			if( orderService.updateReceiveMsg(bean )==1){
				if ( tmp.getStatus() !=5 ){
					if ( Integer.parseInt(orderStatus) == 5){
						Member memberBean=  memberService.getMemberByKey( bean.getMemberId() );
						int p =memberBean.getPoints() + (int) bean.getTotalCost();
						int p2 = memberBean.getPointsb() +(int) bean.getTotalCost();
						memberBean.setLevel(1);
						memberBean.setPoints(p);
						memberBean.setPointsb(p2);
						memberService.updateMember(memberBean);
					}
				}
				msg = true;
			}else
				msg =false;
	    }
		return SUCCESS;
	}
	
	
	public String orderDetail() throws Exception {
		if (orderSeq!=null && !orderSeq.equals("")){
		receiveMsg = orderService.getReceiveMsgByOrderId(orderSeq);
		orderList = orderService.getOrderByOrderSeq(orderSeq);
		}
		return SUCCESS;
	}
	
	public String orderList() throws Exception {
		return super.execute();
	}
	
	public String queryOrder()throws Exception {
		String whereCondition = "";
		if ( orderStatus != null && !orderStatus.equals("") && !orderStatus.equals("-1") ){
			whereCondition = " AND r.status="+ orderStatus;
		}
		if ( beginDate!=null && !beginDate.equals("") ){
			whereCondition = whereCondition + "  AND r.createTime>='"+beginDate +"' ";
		}
		if ( endDate!=null && !endDate.equals("") ){
			whereCondition = whereCondition + "  AND r.createTime<='"+ endDate  +"'  ";
		}
		qryOrders = orderService.getReceiveByCondition(whereCondition);
		return SUCCESS;
	}
	
	
	public String deleOrder()throws Exception {
		if ( orderSeq!=null && !orderSeq.equals("")){
			orderService.deleteOrderByOrderSeq(orderSeq);
			orderService.delteReceiveMsg(orderSeq);
		}
		return SUCCESS;
	}
	public String getOrderStatus() {
		return orderStatus;
	}
 
	public List<ReceiveMsg> getQryOrders() {
		return qryOrders;
	}
	public void setQryOrders(List<ReceiveMsg> qryOrders) {
		this.qryOrders = qryOrders;
	}
	public String getBeginDate() {
		return beginDate;
	}
	public ReceiveMsg getReceiveMsg() {
		return receiveMsg;
	}

	public List<Order> getOrderList() {
		return orderList;
	}

	public void setReceiveMsg(ReceiveMsg receiveMsg) {
		this.receiveMsg = receiveMsg;
	}

	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}

	public String getEndDate() {
		return endDate;
	}
	public void setOrderStatus(String orderStatus) {
		this.orderStatus = orderStatus;
	}
	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
}
